<?php
$path =  str_replace('/wp-content/plugins/stSocialMediaToolbox/Toolboxes/stFacebookToolbox/cronjob','',dirname(__FILE__));
require($path.'/wp-config.php');
require_once $path.'/wp-content/plugins/stSocialMediaToolbox/Toolboxes/stFacebookToolbox/libs/2.1/classes/facebook/facebook.php';
    global $wpdb;
    $data['cronjob_name']  = 'cronjob Facebook Search';
    $data['toolbox_name'] = 'stFacebookToolbox';
    $data['url'] = dirname(__FILE__).'/search.php';
    $data['status'] = 0;
    $data['ctime'] = date_i18n('Y-m-d H:i:s');
    $wpdb->insert('wp_st_cronjob_log',$data);
    $log_id = $wpdb->get_var("SELECT MAX(id) FROM wp_st_cronjob_log");
        $time = mktime(date_i18n("H"),date_i18n("i"),date_i18n("s"),date_i18n("n"),date_i18n("j"),date_i18n("Y"));
        $last = intval($time) - 24*3600;
        $result = $wpdb->get_results("SELECT * FROM st_likepluginsearchterms");
        foreach($result as $row)
        {
            $url = "http://graph.facebook.com/search?q=".$row->searchterm."&type=post&limit=1000&since=".date('Y-m-d',$last)."T".urlencode(date('H:m:s',$last))."%2B0000";
            $array = json_decode(file_get_contents($url));
            if(count($array) > 0)
            {
                 foreach($array as $value)
                 {
                     foreach($value as $value2)
                     {
                         if($value2->id != '')
                         {
                             $data = array();
                             $data['messageid'] = $value2->id;
                             $data['searchtermid'] = $row->searchtermid;
                             $data['fromuserid'] = $value2->from->id;
                             $data['messagetypeid'] = $value2->type;
                             $data['messagetext'] = $value2->message;
                             $data['picture'] = $value2->picture;
                             $data['link'] = $value2->link;
                             $data['source'] = $value2->source;
                             $data['icon'] = $value2->icon;
                             $data['createdtime'] = $value2->created_time;
                             $data['updatedtime'] = $value2->updated_time;
                             $data['likes'] = $value2->likes->count;
                             $data['date'] = date('d-m-Y',$last);
                             $data['user_id'] = $row->user_id;
                             $type = array('%s','%d','%d','%s','%s','%s','%s','%s','%s','%s','%s','%d','%s','%d');
                             $count = $wpdb->get_var("SELECT COUNT(*) FROM st_likepluginfbmessages WHERE messageid='".$value2->id."' AND user_id =  ".$row->user_id." AND date = '".date('d-m-Y',$last)."'");
                             if(intval($count) == 0)
                             {
                                 $wpdb->insert("st_likepluginfbmessages",$data,$type);
                             }
                              else
                             {
                                 $wpdb->update("st_likepluginfbmessages",$data,array('messageid'=>$value2->id),$type,array('%s'));
                             }
                             if($value2->from->id != '' )
                             {
                                if(count($wpdb->get_results("SELECT * FROM st_likepluginfbusers WHERE uid=".$value2->from->id)) == 0)
                                {
                                        $user_info = array();
                                        //$user = json_decode(file_get_contents("https://graph.facebook.com/".$value2->from->id."?fields=birthday,first_name,last_name,locale,gender"));
                                        $user_info['birthday'] = $user->birthday;
                                        $user_info['currentlocation'] = $user->current_location;
                                        $user_info['firstname'] = $user->first_name;
                                        $user_info['lastname'] = $user->last_name;
                                        $user_info['locale'] = $user->locale;
                                        $user_info['profileurl'] = $user->profile_url;
                                        $user_info['sex'] = $user->gender;
                                        $user_info['timezone'] = $user->timezone;
                                        $user_info['uid'] = $value2->from->id;
                                        $user_info['user_id'] = $row->user_id;
                                        $wpdb->insert("st_likepluginfbusers",$user_info);
                                }
                                $user_id[] = $value2->from->id;
                             }
                           }



                     }
                 }
            }

            // ------------------ get facebook user ----------------
        if(count($user_id) > 0):
        $facebook = new Facebook('69c34f82272a06482b64434e8081759b', '7f8f6dd038d3d426c37fa47ff131f634', true);
        $userInfo = $facebook->api_client->users_getInfo($user_id, array('uid', 'first_name', 'last_name','locale','birthday','sex','current_location','profile_url','timezone'));
        foreach($userInfo as $ro)
        {
            $user_info = array();
                    $user_info['birthday'] = $ro['birthday'];
	   	    $user_info['currentlocation'] = $ro['current_location'];
		    $user_info['firstname'] = $ro['first_name'];
		    $user_info['lastname'] = $ro['last_name'];
		    $user_info['locale'] = $ro['locale'];
		    $user_info['profileurl'] = $ro['profile_url'];
		    $user_info['sex'] = $ro['sex'];
		    $user_info['timezone'] = $ro['timezone'];
                    $user_info['uid'] = $ro['uid'];
                    $user_info['user_id'] = $row->user_id;
		    $type = array('%s','%s','%s','%s','%s','%s','%s','%d','%d', '%d');
                    if(count($wpdb->get_results("SELECT * FROM st_likepluginfbusers WHERE uid=".$ro['uid']." AND user_id = ".$row->user_id)) > 0)
                    {
                        $wpdb->update('st_likepluginfbusers',$user_info,array('uid'=>$ro['uid']),$type,array('%d'));
                    }
                    else
                    {
                        $wpdb->insert('st_likepluginfbusers',$user_info,$type);
                    }
                    if($wpdb->get_var("SELECT COUNT(*) FROM st_fbuser_relation WHERE system_user_id  = ".$current_user->ID." AND facebook_user_id = ".$ro['uid']) == 0){
                        $wpdb->insert('st_fbuser_relation',array('system_user_id'=>$current_user->ID,'facebook_user_id'=>$ro['uid']));
                    }
        }
        $user_id = array();
        endif;
        }
$wpdb->update('wp_st_cronjob_log',array('status'=>1),array('id'=>$log_id));
    ?>